import axios from 'axios';
import moment from 'moment';

// 1. 初始状态.
const state = () => ({
    events: [],
});

// 2. 计算属性.
const getters = {};

// 3. 异步修改.
const actions = {
    loadEvents({ state }) {
        axios.get('/instanceEvent').then((res) => {
            state.events = res.data.map((item) => {
                const { createTime, ...rest } = item;

                // 转换时间格式.
                rest.createTime = moment(createTime * 1000).fromNow();
                return rest;
            });
        });
    },
};

// 4. 同步修改.
const mutations = {
};

// 5. 模块导出.
export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations,
};
